//
//  ViewController.m
//  MyTextView
//
//  Created by TomPro on 15/10/21.
//  Copyright © 2015年 TomPro. All rights reserved.
//

#import "ViewController.h"

#import "RegexUtil.h"
#import "AttributeLabel.h"

#import <objc/runtime.h>

static BOOL showKey;

@interface ViewController ()<AttributeLabelDelegate>
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpLabel];
}

- (void)setUpLabel{
    AttributeLabel *myLabel = [[AttributeLabel alloc] init];
    NSAttributedString *attributedStr = [RegexUtil attributeStringWithText:@"你是我的小苹果，小呀小苹果，怎么爱你http://www.baidu.com,我电话号码是是是15529337800" withTextFont:[UIFont systemFontOfSize:15]];
    myLabel.attributedStr = attributedStr;
    CGSize size = [attributedStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:1 context:NULL].size;
    myLabel.frame = CGRectMake(0, 60, size.width, size.height);
    myLabel.delegate = self;
    myLabel.delegate.show = YES;
    [self.view addSubview:myLabel];
}

#pragma mark - AttributeLabelDelegate
- (void)attributeLabelClick:(RegexSepcialText *)sepcialText{
    if (sepcialText.textType == TextTypeLink) { // url
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:sepcialText.text]];
    }else if (sepcialText.textType == TextTypeMobile){ // 电话
        NSString *mobileStr = [NSString stringWithFormat:@"tel://%@",sepcialText.text];
        NSURL *url  = [NSURL URLWithString:mobileStr];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mobileStr]];
        } else {
            NSLog(@"模拟器不能拨打电话");
        }
    }
}
- (void)setShow:(BOOL)show{
    objc_setAssociatedObject(self, &showKey,
                             @(show),
                             OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)show{
    return [objc_getAssociatedObject(self, &showKey) boolValue];
}
@end
